<template>
	<form-container :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
			<a-form-item label="编码：" name="code">
				<a-input v-model:value="formState.code" placeholder="请输入编码" allow-clear />
			</a-form-item>
		</a-form>

		<a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formState.name" placeholder="请输入名称" allow-clear />
			</a-form-item>
		</a-form>

		<a-form-item label="类型：" name="type">
			<a-select v-model:value="formState.type" placeholder="请选择类型">
				<a-select-option value="一般">一般</a-select-option>
				<a-select-option value="测试">测试</a-select-option>
				<a-select-option value="维修">维修</a-select-option>
				<a-select-option value="报废">报废</a-select-option>
				<a-select-option value="返工">返工</a-select-option>
				<a-select-option value="备料">备料</a-select-option>
			</a-select>
		</a-form-item>
		<a-form-item label="报工模式：" name="typeName">
			<a-radio-group v-model:value="formState.typeName">
				<a-radio value="数量">数量</a-radio>
				<a-radio value="状态">状态</a-radio>
			</a-radio-group>
		</a-form-item>

		<a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
			<a-form-item label="描述：" name="description">
				<a-input v-model:value="formState.description" placeholder="请输入描述" allow-clear />
			</a-form-item>
		</a-form>

		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="updatePassword">
	import { required } from '@/utils/formRules'
	import ProcessOperationApi from '@/api/dev/ProcessOperationApi.js'

	// 定义emit事件
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	let visible = ref(false)
	const formRef = ref()
	// 表单数据
	const formState = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (email) => {
		visible.value = true
		formState.value = email
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 默认要校验的
	const rules = {
		code: [required('请输入编号')],
		name: [required('请输入名称')],
		type: [required('请输入类型')],
		typeName: [required('请输入报工模式')],
		description: [required('请输入描述')]
	}

	// 提交数据
	const onSubmit = async () => {
		formRef.value.validateFields().then(() => {
			submitLoading.value = true
			let fromData = {
				id: formState.value.id,
				code: formState.value.code,
				name: formState.value.name,
				type: formState.value.type,
				typeName: formState.value.typeName,
				description: formState.value.description
			}
			// 提交数据
			ProcessOperationApi.processOperationDetail(fromData)
				.then(() => {
					formRef.value.resetFields()
					visible.value = false
					emit('successful', fromData)
				})
				.finally(() => {
					submitLoading.value = false
					callback() //调用组件中的回调方法
				})
		})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped></style>
